#include <stdio.h>
#include <pthread.h>
#include <malloc.h>
#include <stdlib.h>
#include "timer.h"
#include "sleep.h"
#include "cpu.h"

void  * timerThreadAction(Timer);

Timer initializeTimer(void (*function_ptr), CPU_TIMER cpu) {
	TimerStr* str;
	Timer t;
	str = (TimerStr*) malloc(sizeof(TimerStr));
	t = str;
	t->ms = DEFAULT_MS;
	t->function_ptr = function_ptr;
	t->cpu = cpu;
	return t;
}

void startTimer(Timer t) {
	pthread_t pth;
	pthread_create(&pth, NULL, timerThreadAction, t);
	return;
}

void * timerThreadAction(Timer t) {
	void * ret;
	while(1) {
		(t->function_ptr)(t->cpu);
		milliSleep(t->ms);
	}
	return ret;
}
